/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import java.util.Date;
import java.util.List;
import junit.framework.Assert;
import nl.cloudfarming.client.messaging.Message;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Gerben Feenstra
 */
public class MessageInboxIT {
    
    MessageInbox service = new MessageInbox();
    
    public MessageInboxIT() {
    }
    
    @Before
    public void setUp() {
        
        Message.Builder message1 = new Message.Builder().setOrigin("Origination person 1").setTitle("Title1").setMessageText("MessageText1").setDateReceived(new Date());
        Message.Builder message2 = new Message.Builder().setOrigin("Origination person 2").setTitle("Title2").setMessageText("MessageText2").setDateReceived(new Date());
        service.addMessageToInbox(message1.build());       
        service.addMessageToInbox(message2.build());        
    }

    /**
     * Tests the creation and persisting of a message.
     */
    @Test
    public void testCreateMessage() {
        int before = service.getMessages().size();
        Message.Builder message3 = new Message.Builder().setOrigin("Origination person 3").setTitle("Title3").setMessageText("MessageText3").setDateReceived(new Date());       
        Message message = message3.build();
        service.addMessageToInbox(message);
        List<Message> result = service.getMessages();  
        Assert.assertEquals(before + 1, result.size()); // @Before is called twice + message3 created in this test-method = 5
        Assert.assertTrue(result.contains(message));
    }

    /**
     * Tests updating of a message by setting isRead to true (Message has been read)
     */
    @Test
    public void testUpdateMessage() {
        List<Message> result = service.getMessages();
        Message message = result.get(0);
        message.setRead(true);
        service.updateMessage(message);
        
        boolean isAsserted = false;
        for(Message updatedMessage : service.getMessages()){
            if(updatedMessage.getId() == message.getId()){
                Assert.assertTrue(updatedMessage.isRead());
                isAsserted = true;
            }
        }
        Assert.assertTrue(isAsserted);
        // Notifier should be removed if message is read from the inbox
    }
}
